from django.http import JsonResponse

from Friend.models import Friend
from User.models import User


def createFriend(request):
    __mail = request.POST.get("mail")
    __friendMail = request.POST.get("friendMail")
    f = Friend.objects.filter(mail=__mail, friendMail=__friendMail)
    if f.count() > 0:
        return JsonResponse({"msg": "你们已经是好友了"})
    else:
        Friend.objects.create(mail=__mail, friendMail=__friendMail, remark=request.POST.get("remark"),
                              friendInfo=User.objects.filter(mail=__friendMail).first()).save()
        Friend.objects.create(friendMail=__mail, mail=__friendMail,
                              friendInfo=User.objects.filter(mail=__friendMail).first()).save()
        return JsonResponse({"msg": "添加好友成功"})


def isMyFriend(request):
    __me_mail = request.POST.get("me_mail")
    __other_mail = request.POST.get("other_mail")
    f = Friend.objects.filter(mail=__me_mail, friendMail=__other_mail)
    if f.count() > 0:
        return JsonResponse({"msg": "true"})
    else:
        return JsonResponse({"msg": "false"})
